import { request } from '@umijs/max';

/* 登录 */
export async function loginApi(body: any, options?: { [key: string]: any }) {
  const formData = new FormData();

  Object.keys(body).forEach((ele) => {
    const item = (body as any)[ele];

    if (item !== undefined && item !== null) {
      if (typeof item === 'object' && !(item instanceof File)) {
        if (item instanceof Array) {
          item.forEach((f) => formData.append(ele, f || ''));
        } else {
          formData.append(ele, JSON.stringify(item));
        }
      } else {
        formData.append(ele, item);
      }
    }
  });

  return request<any>('/user/login', {
    method: 'POST',
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    data: body,
    ...(options || {}),
  });
}

// 取得分店資料
export async function getBranchList() {
  return request<any>('?page=branchListJ', {
    method: 'GET',
  });
}

//取得商品類別?page=categoryQListJ
export async function getCategoryList() {
  return request<any>('?page=categoryQListJ', {
    method: 'GET',
  });
}

// 新增商品類別 ?page=json
export async function addCategory(body: any) {
  return request<any>(`?page=json`, {
    method: 'POST',
    data: body,
    headers: {
      'Content-Type': 'application/json',
    },
  });
}

// 取得商品類別資訊  ?page=selectProductCategory&oid=oid
export async function getCategoryDetail({ oid }: any) {
  return request<any>(`?page=selectProductCategory&oid=${oid}`, {
    method: 'GET',
  });
}

/* 获取验证码 */
export async function getCaptchaApi(
  body: {
    mobile: string;
  },
  options?: { [key: string]: any },
) {
  return request<any>('/common/code', {
    method: 'GET',
    params: body,
    ...(options || {}),
  });
}

// 获取审核列表
export async function getAuditListApi(body) {
  return request<any>('/program/list', {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 审核列表删除
export async function delectAuditDeleteApi(body) {
  return request<any>(`/program/list/${body?.id}`, {
    method: 'DELETE',
    params: body,
    // ...(options || {}),
  });
}

// 审核列表审核通过
export async function delectAuditPassApi(body) {
  return request<any>(`/program/list/auditing/${body?.id}`, {
    method: 'PUT',
    params: body,
    // ...(options || {}),
  });
}

// 审核列表驳回
export async function delectAuditRejectApi(body) {
  return request<any>(`/program/list/reject/${body?.id}`, {
    method: 'PUT',
    params: body,
    // ...(options || {}),
  });
}

//商户清单列表
export async function getMerchantListApi(body) {
  return request<any>(`/program/inventory`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 修改商户清单
export async function updateMerchantListApi(id, body) {
  return request<any>(`/program/list/${id}`, {
    method: 'PUT',
    data: body,
  });
}

// 个人信息
export async function getUserInfoApi() {
  return request<any>('/user/detail', {
    method: 'GET',
    // params: body,
    // ...(options || {}),
  });
}

// 绑定规则列表
export async function getBindRuleListApi(body) {
  return request<any>(`/battery/binding`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

/* 公告 */
export async function getNoticeListApi(body) {
  return request<any>(`/board/notice`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

/* 新增公告 */
export async function addNoticeApi(body) {
  return request<any>(`/board/notice`, {
    method: 'POST',
    data: body,
    headers: {
      'Content-Type': 'application/json',
    },
  });
}

// 新增绑定规则
export async function addBindRuleApi(body) {
  return request<any>(`/battery/binding`, {
    method: 'POST',
    data: body,
    headers: {
      'Content-Type': 'application/json',
    },
  });
}

// 删除规则绑定吧
export async function delectBindRuleApi(body) {
  return request<any>(`/battery/binding/${body?.id}`, {
    method: 'DELETE',
    params: body,
    // ...(options || {}),
  });
}
/* 删除公告 */
export async function delectNoticeApi(body) {
  return request<any>(`/board/notice/${body?.id}`, {
    method: 'DELETE',
    params: body,
    // ...(options || {}),
  });
}

/* 修改公告 */
export async function updateNoticeApi(id, body) {
  return request<any>(`/board/notice/${id}`, {
    method: 'PUT',
    data: body,
    // ...(options || {}),
  });
}

// 修改绑定规则
export async function updateBindRuleApi(id, body) {
  return request<any>(`/battery/binding/${id}`, {
    method: 'PUT',
    data: body,
    // ...(options || {}),
  });
}

// 优惠券列表
export async function getCouponListApi(body) {
  return request<any>(`/battery/coupon-rule`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

export async function addCoupon(body) {
  return request<any>(`/battery/coupon-rule`, {
    method: 'POST',
    data: body,
    headers: {
      'Content-Type': 'application/json',
    },
    // ...(options || {}),
  });
}

export async function updateCouponRuleApi(id, body) {
  return request<any>(`/battery/coupon-rule/${id}`, {
    method: 'PUT',
    data: body,
    // ...(options || {}),
  });
}

// 删除
export async function delectCouponApi(body) {
  return request<any>(`/battery/coupon-rule/${body?.id}`, {
    method: 'DELETE',
    params: body,
    // ...(options || {}),
  });
}

// 商户配置项
export async function getProgramOptions() {
  return request<any>(`/program/select`, {
    method: 'GET',
    // ...(options || {}),
  });
}

// 优惠券日志接口
export async function getCouponLog(body) {
  return request<any>(`/journal/coupons`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 发展用户列表
export async function getDevelopUserList(body) {
  return request<any>(`/battery/develop-list`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 在租用户
export async function getRentUserList(body) {
  return request<any>(`/battery/renting-list`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 逾期
export async function getOverdueUserList(body) {
  return request<any>(`/battery/overdue-list`, {
    method: 'GET',
    params: body,
    // ...(options || {}),
  });
}

// 在租
export async function importRentUserList(body) {
  const formData = new FormData();

  Object.keys(body).forEach((ele) => {
    const item = (body as any)[ele];

    if (item !== undefined && item !== null) {
      if (typeof item === 'object' && !(item instanceof File)) {
        if (item instanceof Array) {
          item.forEach((f) => formData.append(ele, f || ''));
        } else {
          formData.append(ele, JSON.stringify(item));
        }
      } else {
        formData.append(ele, item);
      }
    }
  });

  return request<any>(`/battery/import-excel`, {
    method: 'POST',
    data: formData,
    // ...(options || {}),
  });
}
